/**
 * 基础配置-单位配置表
 * @author xujie
 * @since 2018/10/18
 */
layui.define(['index', 'form', 'table', 'laypage', 'layedit'], function (exports) {

    let $ = layui.$;
    let form = layui.form;
    let layer = layui.layer;
    let table = layui.table;
    let laypage = layui.laypage;
    let layedit = layui.layedit;

    table.init('zmn-table', {
        toolbar: "#zmn-table-tool",
        page: true,
        limit: json.pageSize,
        height: 'full-149'
    });

    laypage.render(layui.$.extend({limits:[10, 20, 30, 40, 50,100]},zmnPage.laypageOptions(json)));

    let ids = [];

    // 选择（全选或单选）
    table.on("checkbox(zmn-table)", function (obj) {
        ids = table.checkStatus('zmn-table').data.map((item) => item.unitId);
    });

    table.on("toolbar(zmn-table)", function (obj) {
        //启用停用
        if (obj.event === "add") {
            let title = '新增单位配置';
            layer.open({
                type: 2,
                offset: '15%',
                area: ['500px', '400px'],
                title: [title],
                content:["/unit/addEditView.action", 'no'],
                end: function () {
                    window.location.reload();
                }
            });
        }
        if (obj.event === "delete") {
            if (ids.length === 0) {
                return layer.alert("请至少选择1条数据！");
            } else {
                layer.confirm('确定要删除选中的单位名称吗？', {btn: ['确认', '取消'], title: '删除'}, function (index) {
                    layer.close(index);
                    $.ajax({
                        async: false,
                        type: "POST", url: "/unit/deleteBatch.action",
                        contentType: "application/json", dataType: "json", data: JSON.stringify(ids),
                        success:function(result) {
                            if(result.status === 200) {
                                zmnPage.reload(result.message, 1);
                            } else {
                                layer.msg(result.message, {"icon": 2});
                            }
                        },
                        error:function(XMLHttpRequest, textStatus, errorThrown){
                        }
                    })
                });
            }
        }

    });

    //修改排序
    table.on('edit(zmn-table)', function (obj) {

        let pattern = /^\+?[0-9][0-9]*$/;
        if (!pattern.test(obj.value) || parseInt(obj.value) < 0 || parseInt(obj.value) > 2147483647) {
            layer.msg("请输入0~2147483647的正整数", {"icon": 2});
            return false;
        }
        let data = {
            unitId: obj.data.unitId,
            unitName: obj.data.unitName,
            sort: obj.value
        }
        let url = "/unit/addEdit.action";
        $.ajax({
            async: false,
            type: "POST", url: url,
            contentType: "application/json", dataType: "json", data: JSON.stringify(data),
            success:function(result) {
                if(result.status === 200) {
                    zmnPage.reload(result.message, 1);
                } else {
                    layer.msg(result.message, {"icon": 2});
                }
            },
            error:function(XMLHttpRequest, textStatus, errorThrown){
            }
        })
    });

    table.on('tool(zmn-table)', function(obj){
        let data = obj.data;
        let layEvent = obj.event;
        let tr = obj.tr;
        if(layEvent === 'update'){
            let title = '修改单位配置';
            let unitId = data.unitId;
            layer.open({
                type: 2,
                offset: '15%',
                area: ['500px', '500px'],
                title: [title],
                content:["/unit/addEditView.action?unitId="+unitId, 'no'],
                end: function () {
                    window.location.reload();
                }
            });
        }
    });

    $(document).on('click', '#zmn-form-reset ', function () {

        $('#unitName').val("");
        form.render();
    })

    //对外暴露的接口
    exports('plat_unit_list', {});
});

